package com.xixibaby.logistic;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xixibaby.core.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

/**
 * @author tomj
 */
public class KuaiDi100Kit {

    private static final Logger logger = LoggerFactory.getLogger("KuaiDi100Kit");

    private KuaiDi100Config config;

    public KuaiDi100Config getConfig() {
        return config;
    }

    public void setConfig(KuaiDi100Config config) {
        this.config = config;
    }


    public JSONObject query(String com, String num){
        if(!Assert.notEmpty(this.getConfig())){
            logger.warn("快递100不能连接，配置文件不存在！");
            return null;
        }
        JSONObject object = new JSONObject();
        object.put("com",com);
		object.put("num",num);

		String param =object.toJSONString();
		String customer = this.getConfig().getCustomer();
		String key = this.getConfig().getKey();
        String sign = SecureUtil.md5(param + key + customer).toUpperCase();
        HashMap<String,Object> params = new HashMap<>();
		params.put("param",param);
		params.put("sign",sign);
		params.put("customer",customer);
		try {
			String post = HttpUtil.post(this.getConfig().getQueryUrl(), params);
            return JSONObject.parseObject(post);
        } catch (Exception e) {
			logger.error("快递100查询出错！",e);
		}
		return null;
    }
}
